<template>
    <view class="info">
        <view class="header">
            <image class="infoimg" mode="aspectFill" :src="spinfo.img"></image>
        </view>
        <view class="infoname">
            <view class="infotitle">
                <view>{{ spinfo.name }}</view>
                <view class="sysl">剩余{{ spinfo.number }}个</view>
            </view>
            <view class="infocost">
                <text class="money">{{ spinfo.money }}</text>
                <text>积分</text>
                <text class="money1" v-if="spinfo.type == '1'">￥{{ spinfo.hb_moeny }}</text>
            </view>
        </view>
        <view class="section">
            <view class="secheader">详情说明</view>
            <view class="sectioninfo">
                <view>
                    <view class="infotitle">商品简介</view>
                    <rich-text :nodes="spinfo.goods_details"></rich-text>
                </view>
                <view>
                    <view class="infotitle">兑换流程</view>
                    <rich-text :nodes="spinfo.process_details"></rich-text>
                </view>
                <view>
                    <view class="infotitle">注意事项</view>
                    <rich-text :nodes="spinfo.attention_details"></rich-text>
                </view>
            </view>
        </view>
        <view class="foot"></view>
        <view class="footer">
            <button class="footbtn" style="opacity: 0.6" v-if="spinfo.number == '0'">抱歉！您来晚了</button>
            <button @tap="duihuan" class="footbtn" :disabled="djdh" :style="'background:' + color" v-else>马上兑换</button>
        </view>
        <view class="bomb flex3" v-if="!bomb">
            <view class="bomb_box">
                <view class="bomb_title">确定使用{{ spinfo.money }}积分兑换？</view>
                <view class="bomb_con" v-if="spinfo.type == '1'">
                    兑换成功后将自动为您余额充值
                    <text style="color: #ed414a">{{ spinfo.hb_moeny }}</text>
                </view>
                <view class="bomb_con" v-if="spinfo.type == '2'">兑换实物将使用您的收货地址邮寄给您</view>
                <view class="bomb_foot">
                    <view @tap="cancel" class="cancel">取消</view>
                    <view @tap="caomfirm" class="caomfirm" v-if="spinfo.type == '1'">兑换</view>
                    <view @tap="caomfirm" class="caomfirm" v-if="spinfo.type == '2'">兑换</view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index';
import gettel from '@/ymq_canyino2o/dist/gettel/index';
var app = getApp();
export default {
    components: {
        cardlist,
        gettel
    },
    data() {
        return {
            bomb: true,
            kpgg: true,
            ssq: '',
            xxdz: '',
            djdh: false,
            qddh: false,

            spinfo: {
                img: '',
                name: '',
                number: '',
                money: '',
                type: '',
                hb_moeny: '',
                goods_details: '',
                process_details: '',
                attention_details: ''
            },

            integral: '',
            myaddress: '',
            location: '',
            color: ''
        };
    },
    onLoad: function (t) {
        console.log(t.id);
        app.globalData.setNavigationBarColor(this);
        uni.hideShareMenu({});
        var e = uni.getStorageSync('users').id;
        var that = this;
        app.globalData.util.request({
            url: 'entry/wxapp/JfGoodsInfo',
            cachetime: '0',
            data: {
                id: t.id
            },
            success: function (t) {
                console.log(t);
                that.setData({
                    spinfo: t.data[0]
                });
                uni.setNavigationBarTitle({
                    title: t.data[0].name
                });
            }
        });
        app.globalData.util.request({
            url: 'entry/wxapp/UserInfo',
            cachetime: '0',
            data: {
                user_id: e
            },
            success: function (t) {
                console.log(t);
                that.setData({
                    integral: t.data.total_score
                });
            }
        });
        app.globalData.util.request({
            url: 'entry/wxapp/MyDefaultAddress',
            cachetime: '0',
            data: {
                user_id: e
            },
            success: function (t) {
                console.log(t.data);
                if (t.data) {
                    that.setData({
                        myaddress: t.data
                    });
                }
            }
        });
    },
    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    onShareAppMessage: function () {},
    methods: {
        duihuan: function () {
            this.setData({
                bomb: false
            });
        },

        cancel: function () {
            this.setData({
                bomb: true
            });
        },

        caomfirm: function () {
            var t = uni.getStorageSync('users').id;
            var e = this.myaddress;
            var that = this;
            var o = that.spinfo.id;
            var s = that.spinfo.money;
            var n = that.spinfo.hb_moeny;
            var i = Number(that.integral);
            var d = that.spinfo.name;
            var l = that.spinfo.img;
            console.log(e, t, o, Number(s), n, i, d, l);
            if ('1' == that.spinfo.type) {
            } else {
                that.setData({
                    bomb: true
                });
                if (Number(s) > i)
                    uni.showModal({
                        title: '提示',
                        content: '您的积分不足以兑换此物品'
                    });
                else if (null == e) {
                    uni.showModal({
                        title: '提示',
                        content: '请前往个人中心填写收货地址',
                        success: function (t) {
                            if (t.confirm) {
                                console.log('用户点击确定');
                                uni.reLaunch({
                                    url: '../../my2/index'
                                });
                            } else {
                                if (t.cancel) {
                                    console.log('用户点击取消');
                                }
                            }
                        }
                    });
                } else {
                    var c = e.user_name;
                    var u = e.tel;
                    var r = e.area + e.address;
                    console.log(c, u, r);
                    uni.showLoading({
                        title: '提交中',
                        mask: true
                    });
                    app.globalData.util.request({
                        url: 'entry/wxapp/Exchange',
                        cachetime: '0',
                        data: {
                            user_id: t,
                            good_id: o,
                            integral: s,
                            user_name: c,
                            user_tel: u,
                            address: r,
                            type: 2,
                            good_name: d,
                            good_img: l
                        },
                        success: function (t) {
                            console.log(t);
                            if (1 == t.data) {
                                uni.showToast({
                                    title: '兑换成功'
                                });
                                setTimeout(function () {
                                    uni.navigateBack({});
                                }, 1000);
                            } else {
                                uni.showToast({
                                    title: '请重试！',
                                    icon: 'loading'
                                });
                                that.setData({
                                    qddh: false
                                });
                            }
                        }
                    });
                }
            }
        },

        ycgg: function () {
            this.setData({
                kpgg: true
            });
        },

        dingwei: function (t) {
            console.log(t);
            var that = this;
            uni.chooseLocation({
                success: function (t) {
                    console.log(t);
                    var e = t.address.indexOf('区');
                    console.log(t.address.substring(0, e + 1));
                    that.setData({
                        location: t.latitude + ',' + t.longitude,
                        ssq: t.address.substring(0, e + 1),
                        xxdz: t.address.substring(e + 1) + t.name
                    });
                }
            });
        },

        formSubmit: function (t) {
            console.log('form发生了submit事件，携带数据为：', t.detail.value);
            var that = this;
            var a = uni.getStorageSync('users').id;
            var o = that.spinfo.id;
            var s = that.spinfo.money;
            var n = that.spinfo.name;
            var i = that.spinfo.img;
            var d = t.detail.value.lxr;
            var l = t.detail.value.tel;
            var c = (that.ssq, that.ssq + t.detail.value.grxxdz);
            console.log(a, o, s, d, l, c, n, i);
            var u = '';
            var r = true;
            if ('' == d) {
                u = '请填写联系人！';
            } else {
                if ('' == l) {
                    u = '请填写联系电话！';
                } else {
                    if (/^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/.test(l) && 11 == l.length) {
                        if ('' == c) {
                            u = '请选择位置！';
                        } else {
                            r = false;
                            that.setData({
                                qddh: true
                            });
                            app.globalData.util.request({
                                url: 'entry/wxapp/Exchange',
                                cachetime: '0',
                                data: {
                                    user_id: a,
                                    good_id: o,
                                    integral: s,
                                    user_name: d,
                                    user_tel: l,
                                    address: c,
                                    type: 2,
                                    good_name: n,
                                    good_img: i
                                },
                                success: function (t) {
                                    console.log(t);
                                    if (1 == t.data) {
                                        uni.showToast({
                                            title: '兑换成功'
                                        });
                                        setTimeout(function () {
                                            uni.navigateBack({});
                                        }, 1000);
                                    } else {
                                        uni.showToast({
                                            title: '请重试！',
                                            icon: 'loading'
                                        });
                                        that.setData({
                                            qddh: false
                                        });
                                    }
                                }
                            });
                        }
                    } else {
                        u = '手机号错误';
                    }
                }
            }
            if (1 == r) {
                uni.showModal({
                    title: '提示',
                    content: u
                });
            }
        }
    }
};
</script>
<style>
@import './integralinfo.css';
</style>
